import { Router } from 'vue-router'
import _import from 'src/import-module'

function convertName(href: string) {
  if (href) {
    let hrefs = href.split('/')
    hrefs = hrefs.filter(item => item)
    return hrefs[hrefs.length - 1]
  }
  return ''
}

interface Menu {
  name: string
  href: string
}

function convertRoute(router: Router, menus: Array<Menu> = []) {
  menus.forEach((item, index) => {
    const route = {
      path: item.href || '',
      name: convertName(item.href) || '',
      component: _import(item.href),
      meta: { title: item.name }
    }
    router.addRoute('index', route)
  })
}

export default convertRoute
